<template>
  <div>
    <left-left style="width: 93%"/>
    <team-manage-left style="margin-left: 5%;position: absolute;top:0;width: 65%"/>
    <el-container style="position:absolute;top:0;margin-left: 18%;height: 100vh">
      <el-aside>
        <el-divider class="el-divider1" direction="vertical" style="height: 1000%"></el-divider>
      </el-aside>


      <el-main >

        <div style="margin-left: 2%;margin-top: 3%;font-size: 20px">通过邮箱邀请</div>
        <div style="margin-left: 2.2%;margin-top: 0.8%;font-size: 13px">你可以直接添加已注册的Team用户加入你的团队，若对方未注册，会向其发</div>
        <div style="margin-left: 2.2%;font-size: 13px">送邀请邮件</div>
        <el-divider></el-divider>
        <div><span style="margin-left: 8.5%">邮箱</span><span style="margin-left: 20.5%">姓名</span><span style="margin-left: 20%">身份</span></div>
        <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic" style="margin-top: 1%">

          <el-form-item
            v-for="(domain, index) in dynamicValidateForm.domains"
            :label="'新增成员' +(index+1)"
            :key="domain.key"
            :prop="'domains.' + index + '.value'"
            :rules="rules"
          >
            <el-input placeholder="请输入邮箱" v-model="domain.email" style="width: 20%"></el-input>
            <el-input placeholder="请输入姓名" v-model="domain.targetName" style="width: 20%;margin-left: 5%" ></el-input>


            <el-select v-model="domain.identity" placeholder="身份" style="margin-left: 5%;width: 10%">
              <el-option
                v-for="item in allStatus "
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-button style="margin-left: 3%" type="danger" @click.prevent="removeDomain(domain)">删除</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="addDomain" type="primary">再加一个</el-button>
          </el-form-item>
        </el-form>
        <el-divider></el-divider>
        <el-button style="margin-left: 40%" type="success" @click="submitForm('dynamicValidateForm')">提交</el-button>
      </el-main>




    </el-container>


  </div>
</template>

<script>
import leftLeft from '@/components/left/leftLeft.vue'
import teamManageLeft from "@/components/left/teamManageLeft";
export default {
  name: 'teammateManagement',
  components: {
    leftLeft,
    teamManageLeft
  },
  data(){
    return{
      dynamicValidateForm: {
        domains: [{
          name:'',
          identity:'',
          targetName:'',
          email: ''
        }],
      },
      rules:{
        email:[{ required: true, message: '邮箱不能为空', trigger: 'blur' },]
      },
      allStatus: [{
        value: '管理员',
        label: '管理员'
      }, {
        value: '成员',
        label: '成员'
      }],
      value: '',
    }
  },
  created() {
    this.$store.state.teamID=JSON.parse(localStorage.getItem("insuranceTeamCode"));
    this.$store.state.ID=JSON.parse(localStorage.getItem("insuranceUserCode"));
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const formData=new FormData();
          formData.append("teamID",this.$store.state.teamID)
          formData.append("userID",this.$store.state.ID)
          formData.append("params",JSON.stringify(this.dynamicValidateForm))
          this.$axios({
            method: 'post',           /* 指明请求方式，可以是 get 或 post */
            url: '/team/invite1',       /* 指明后端 api 路径，由于在 main.js 已指定根路径，因此在此处只需写相对路由 */
            data: formData
          })
            .then(res => {              /* res 是 response 的缩写 */
              this.result = res.data.data
              window.alert(JSON.stringify(res.data))
            })
            .catch(err => {
              /* 若出现异常则在终端输出相关信息 */
            })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    removeDomain(item) {
      var index = this.dynamicValidateForm.domains.indexOf(item)
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1)
      }
    },
    addDomain() {
      this.dynamicValidateForm.domains.push({
        value: '',
        key: Date.now()
      });
    }
  }

}
</script>

<style scoped>
.el-dropdown {
  vertical-align: top;
}
.el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.el-divider1{
  height: 100vh;
}
.el-main{
  margin-left: -23.3%;
  width: 950px;
}
.el-button {
  border-right:0!important;
  border-left:0!important;
  border-top:0!important;
  border-bottom:0!important;
}
</style>
